package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.system.LoginLog;
import com.qingcheng.service.system.LoginLogService;

import com.qingcheng.util.WebUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Welcome to the most Amazing IntelliJ IIIDEA.
 * Creat0r: ^_^ fffantianxing ^_^
 * Date: 2019-06-29
 * Time: 15:35
 */
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

    @Reference
    private LoginLogService loginLogService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
//        httpServletResponse.setStatus(200);
        LoginLog loginLog = new LoginLog();
        loginLog.setLoginName(authentication.getName());//设置登陆管理员名字
        loginLog.setIp(httpServletRequest.getRemoteAddr());//设置登陆ip
        loginLog.setLocation(WebUtil.getCityByIP(httpServletRequest.getRemoteAddr()));//设置登陆地点
        loginLog.setLoginTime(new Date());//设置登陆时间
        loginLog.setBrowserName(WebUtil.getBrowserName(httpServletRequest.getHeader("user-agent")));//设置登陆浏览器
        loginLogService.add(loginLog);

        httpServletRequest.getRequestDispatcher("main.html").forward(httpServletRequest,httpServletResponse);



    }
}
